let myaudio = wx.createInnerAudioContext({
        useWebAudioImplement: true
    }),
    innerAudioContext = wx.createInnerAudioContext(),
    definition = '',
    word = '',
    pron = '',
    idx = 0,
    spellWord = '',
    wordName = {},
    limitCnt = 0,
    readList = [],
    phrase = '';
Page({
    data: {
        token: '25.ebe9c9603ca0234a63139fdb1130deef.315360000.1967018034.282335-26133913', //百度的token
        is_play: true,
        idx: 0,
        arr: []
    },

    async onLoad() {
        readList = wx.getStorageSync('selectData')
        console.log('readList', readList)
        this.setData({
            arr: readList
        })
        console.log(this.data.arr, readList[idx])
        // return
        // myaudio.src = 'https://dict.youdao.com/dictvoice?audio=' + readList[idx] + '&type=2'
        // myaudio.play()
        await this.read(idx)
        await this.playAudio()
        //监听各个阶段
        innerAudioContext.onCanplay(() => {
            console.log('可以播放');
        });
        innerAudioContext.onError((res) => {
            console.log(res.errMsg);
            console.log(res.errCode);
        });
        innerAudioContext.onPlay(() => {
            console.log('开始播放')
        });
        await innerAudioContext.onEnded(() => {
            idx += 1
            console.log('播放结束', idx)
            if (idx < readList.length) {
                this.read(idx)
                setTimeout(() => {
                    this.playAudio()
                    console.log("延迟调用this.playAudio()0.8秒");
                }, 800)

                this.setData({
                    idx: idx
                })
            } else {
                idx = 0;
                this.read(idx)
                setTimeout(() => {
                    this.playAudio()
                    console.log("延迟调用this.playAudio()0.8秒");
                }, 800)
                this.setData({
                    idx: idx
                })
            }
        })
    },

    async playAudio(options) {
        limitCnt += 1
        console.log('limitCnt',limitCnt)
        if (limitCnt <= 10) {
            let {
                result
            } = await wx.cloud.callFunction({
                name: 'queryWord',
                data: {
                    content: readList[idx]
                }
            })
            result = result[0]
            // console.log(res)
            // return
            definition = result.definition
            phrase = result.phrase
            spellWord = readList[idx].toUpperCase().split(""); // 分割字符串

            let fen_idx = definition.indexOf("\；"), //获取. 后面 ；前边的字符串
                dian_idx = definition.indexOf("\. "),
                chg_idx = result.phrase.indexOf("\;"),
                phr = phrase.substring(0, chg_idx),
                def = definition.substring(dian_idx + 2, fen_idx);

            this.setData({
                word: readList[idx],
                definition: definition.substring(0, fen_idx), // 究极缝补
                pron: result.pron,
                phrase: phrase.substring(0, chg_idx + 1)
            })
            const appkey = 'OWnY0Ylsuaq4FXKA5jsQNb8E'; //百度应用的 一个月过期
            const selKey = 'd3V1dgqBBpyp3d0WAIz5S4GHFBjmuCss'; //百度应用的 一个月过期 
            wx.request({
                method: 'GET',
                url: `https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=${appkey}&client_secret=${selKey}`,
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success(res) {
                    innerAudioContext.autoplay = true
                    // 播放释义+单词拼写+短语
                    innerAudioContext.src = 'https://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=abcdxxx&tok=' + res.data.access_token + '&tex=' + ',' + def + ',' + spellWord + ',' + phr + '&vol=4&per=4&spd=5&pit=5&aue=3';
                },

            })
            let endTime = 0
            await wx.setStorageSync('limitCount', {
                limitCnt,endTime
            })
        }else{
            innerAudioContext.stop()
            var todayTime = new Date();
            todayTime.setTime(todayTime.getTime(todayTime) + 24 * 60 * 60 * 1000);
            let endTime = todayTime.getTime()
            wx.setStorageSync('limitCount', {
                limitCnt,endTime
            })
            await wx.showModal({
                title: '今天的次数用完了哦',
                showCancel: false,
                confirmText: '好哒',
                success(res) {
                    if (res.confirm) {
                        wx.navigateBack()
                    }
                }
            })
        }
    },

    read(idx) {
        myaudio.src = 'https://dict.youdao.com/dictvoice?audio=' + readList[idx] + '&type=2'
        console.log("read", readList[idx])
        myaudio.play();
    },

    // 点击播放暂停
    onMusicTap: function () {
        const {
            is_play
        } = this.data
        if (is_play) {
            innerAudioContext.pause();
            this.setData({
                is_play: false
            })
            console.log("暂停播放")
        } else if (!is_play) {
            innerAudioContext.play();
            this.setData({
                is_play: true
            })
            console.log("继续播放")
        }
    },

    /**
     * 停止音频文件
     */
    stopAudio() {
        innerAudioContext.stop()
    },

    async bindPickerChange(e) {

        this.setData({
            idx: e.detail.value,
        })
        // this.onLoad()
        console.log('picker发送选择改变，携带值为', this.data.idx)
        idx = Number(this.data.idx)
        // console.log(typeof idx)
        await this.playAudio()
    },

    onUnload() {
        definition = '',
            word = '',
            pron = '',
            idx = 0,
            spellWord = '',
            wordName = {},
            readList = [],
            phrase = '';
        console.log('退出当前界面')
        this.stopAudio()
    }
})